using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class HP_UI : MonoBehaviour
{
    public CharacterStat characterStat;
    private Slider slider;
    private TextMeshProUGUI text;

    private void Start()
    {
        slider = GetComponent<Slider>();
        text = GetComponentInChildren<TextMeshProUGUI>();
        if (characterStat != null) characterStat.onChangeHP += UpdateUI;

        UpdateUI();
    }

    private void UpdateUI()
    {
        int maxValue = characterStat.GetMaxHp();
        int value = characterStat.GetHp();
        slider.maxValue = maxValue;
        slider.value = value;
        text.text = (value.ToString() + "/" + maxValue.ToString());
    }

    private void OnDisable()
    {
        characterStat.onChangeHP -= UpdateUI;
    }
}
